home *** CD-ROM | disk | FTP | other *** search
- /* ===========================
- * AEAAccessorModelFromWild.cc
- * ===========================
- */
-
- #include "AEADebugging.h"
-
- #include "AEAModel.hh"
- #include "AEADescKeyData.hh"
- #include "AEADescList.hh"
- #include "AEAAccessorModelFromWild.hh"
- #include "AEATokenModel.hh"
-
- AEAAccessorModelFromWild::AEAAccessorModelFromWild(DescType inDesiredClass, DescType inContainerType)
- : AEAAccessor(inDesiredClass, inContainerType)
- {
- }
-
- AEAAccessorModelFromWild::AEAAccessorModelFromWild()
- : AEAAccessor(typeModelToken, typeModelToken)
- {
- }
-
- AEAAccessorModelFromWild::~AEAAccessorModelFromWild()
- {
- }
-
- void
- AEAAccessorModelFromWild::AccessObject(DescType inDesiredClass,
- const AEAModelContainer *inContainer, DescType inContainerClass,
- DescType inKeyForm, const AEADescKeyData &inKeyData, AEAModel *&outObject) const
- {
- // Get the containing object
- // Get the selection data
- switch (inKeyForm) {
- case formPropertyID:
- AccessObjectByPropertyID(inDesiredClass, inContainer, inContainerClass,
- inKeyData, outObject);
- break;
- case formName:
- AccessObjectByName(inDesiredClass, inContainer, inContainerClass,
- inKeyData, outObject);
- break;
- case formAbsolutePosition:
- AccessObjectByAbsolutePosition(inDesiredClass,
- inContainer, inContainerClass, inKeyData, outObject);
- break;
- default:
- throw errAEEventNotHandled;
- break;
- }
-
- }
-
- void
- AEAAccessorModelFromWild::AccessObjectByPropertyID(DescType inDesiredClass,
- const AEAModelContainer *inContainer, DescType inContainerClass,
- const AEADescKeyData &inKeyData, AEAModel *&outObject) const
- {
- //ThrowOSErr_(errAEEventNotHandled);
- DescType prop;
- inKeyData.Get(prop);
- inContainer->GetProperty(inDesiredClass, prop, outObject);
- }
-
- void
- AEAAccessorModelFromWild::AccessObjectByName(DescType inDesiredClass,
- const AEAModelContainer *inContainer, DescType inContainerClass,
- const AEADescKeyData &inKeyData, AEAModel *&outObject) const
- {
- ThrowIfNULL_(inContainer);
-
- #if 0
- AEADesc name;
- inKeyData.Get(name);
- #endif
- inContainer->GetElementByName(inDesiredClass, inKeyData, outObject);
-
- if (outObject == NULL)
- ThrowOSErr_(errAENoSuchObject);
- }
-
- #if 0
- void
- AEAAccessorModelFromWild::AccessObjectByName(DescType inDesiredClass,
- const AEAModelContainer *inContainer, DescType inContainerClass,
- const AEADescKeyData &inKeyData, AEAModel *&outObject) const
- {
- ThrowOSErr_(errAEEventNotHandled);
- }
- #endif
-
- void
- AEAAccessorModelFromWild::AccessObjectByAbsolutePosition(DescType inDesiredClass,
- const AEAModelContainer *inContainer, DescType inContainerClass,
- const AEADescKeyData &inKeyData, AEAModel *&outObject) const
- {
- ThrowIfNULL_(inContainer);
-
- long count = inContainer->CountElements(inDesiredClass);
- long index = 0;
- DescType position = typeNull;
- AEAModel *obj = NULL;
-
- if (inKeyData.DescriptorType() == typeAbsoluteOrdinal) {
- inKeyData.Get(position);
- switch (position) {
- case kAEAny: // Fix it later.
- case kAEFirst:
- index = 1;
- break;
- case kAELast:
- index = count;
- break;
- case kAEMiddle:
- index = (count + 1) / 2;
- break;
- default:
- index = 0;
- break;
- }
- } else {
- if (inKeyData.DescriptorType() == typeLongInteger) {
- inKeyData.Get(index);
- } else if (inKeyData.DescriptorType() == typeShortInteger) {
- inKeyData.Get(index);
- }
- if (index < 0)
- index = count + 1 - index;
- }
- if (index > 0) {
- inContainer->GetElementByIndex(inDesiredClass, index, outObject);
- //instance = app->Instance(index);
- //if (obj == NULL) ThrowOSErr_(errAENoSuchObject);
- //outObject = obj;
- } else if (position == kAEAll) {
- // make list
- AEDesc tokenList;
- AEADescList list;
- //AEADesc::AssignDesc(&list, &tokenList);
- // list.Ref() = tokenList; // This can't be right... (unititialized)
- list.Create();
- // On exception, dispose of the list descriptor's data handle.
- //list.SetDestruction(true, false);
- for (int i = 1; i <= count; i++) {
- inContainer->GetElementByIndex(inDesiredClass, i, obj);
- if (obj != NULL) {
- AEATokenModel *token;
- obj->MakeToken(token);
- list.Put(i, typeModelToken, &token, sizeof token);
- }
- }
- // If we got this far, we need the data handle.
- //list.SetDestruction(false, false);
- tokenList = list.Ref();
- list.Reset();
- // Yes! We are *throwing* the list as an exception!
- throw tokenList;
- } else
- ThrowOSErr_(errAENoSuchObject);
- }
-
-
- /*
- */
-
- void
- AEAAccessorModelFromWild::AccessObject(
- DescType inDesiredClass, const AEDesc *inContainerToken, DescType inContainerClass,
- DescType inKeyForm, const AEDesc *inKeyData, AEDesc *outToken) const
- {
- // What should we do if the descriptor type is wrong? Could it even be?
- if (inContainerToken->descriptorType != typeModelToken)
- ThrowOSErr_(errAEWrongDataType);
- // The data handle contains a pointer to the token object.
- AEATokenModel *token = **(AEATokenModel ***)inContainerToken->dataHandle;
- // This shouldn't happen, but check anyway.
- ThrowIfNULL_(token);
-
- AEAModelContainer *container = token->Model();
- const AEADescKeyData keyData(*inKeyData);
- AEAModel *obj;
- try {
- // If we're accessing a list, then an AEDesc exception will be thrown.
- AccessObject(inDesiredClass, container, inContainerClass, inKeyForm, keyData, obj);
- // obj now exists. It could be a temp, so be sure it gets a goodbye kiss.
- try {
- AEATokenModel *token;
- obj->MakeToken(token);
- // token now exists (and is responsible for obj). Delete on exception.
- OSErr err = ::AECreateDesc(typeModelToken, &token, sizeof token, outToken);
- if (err) {
- delete token;
- ThrowOSErr_(err);
- }
- } catch (...) {
- obj->KissGoodbye();
- throw;
- }
- } catch (AEDesc desc) {
- // the key data specified a list of model objects.
- *outToken = desc;
- }
- }
-